import { useState, useEffect } from 'react';

class CurModel {
    static set_lang(db) {
        sessionStorage.setItem("lang", db);
    }

    static get_lang() {
        return CurModel.getItem("lang");
    }

    static get_log() {
        return CurModel.getItem("piclog");
    }

    static set_log(db) {
        sessionStorage.setItem("piclog", db);
    }

    static get_client() {
        return CurModel.getItem("client");
    }

    static set_client(db) {
        sessionStorage.setItem("client", db);
    }

    static get_db(tab) {
        return CurModel.getItem(`mestab__${tab}`);
    }

    static set_db(tab, db) {
        sessionStorage.setItem(`mestab__${tab}`, db);
    }

    static get_cur_wsn() {
        return CurModel.getItem("cur_wsn", "");
    }

    static set_cur_wsn(v) {
        sessionStorage.setItem('cur_wsn', v);
    }

    static get_cur_ver() {
        return CurModel.getItem("cur_ver", "");
    }

    static set_cur_ver(v) {
        sessionStorage.setItem('cur_ver', v);
    }

    static getLine() {
        const line = CurModel.getItem("line", "line-one");
        return line;
    }

    static setLine(cur) {
        sessionStorage.setItem('line', cur);
    }

    static setLineDes(cur) {
        sessionStorage.setItem('linedes', cur);
    }

    static getLineDes() {
        const line = CurModel.getItem("linedes", "");
        return line;
    }

    static getSelect(def) {
        return CurModel.getItem("select", def);
    }

    static setSelect(cur) {
        sessionStorage.setItem('select', cur);
    }

    static setItem(item, def) {
        sessionStorage.setItem(item, def);
    }

    static getItem(item, def) {
        let cur = sessionStorage.getItem(item);
        if (!cur) {
            return def;
        }

        if (cur.length <= 0) {
            return def;
        }

        return cur;
    }
}

class User {
    static addUser(user) {
        sessionStorage.setItem('name', user.name);
        sessionStorage.setItem('uname', user.uname);
        sessionStorage.setItem('login', user.login);
        sessionStorage.setItem('auth', user.auth);
        sessionStorage.setItem('access', user.access);
    }

    static userAuth() {
        let auth = sessionStorage.getItem('auth');
        return auth;
    }

    static userAccess() {
        let tmp = sessionStorage.getItem('access');
        return tmp;
    }

    static uName() {
        let name = sessionStorage.getItem('uname');
        return name;
    }

    static userName() {
        let name = sessionStorage.getItem('name');
        return name;
    }

    static LogOut() {
        sessionStorage.setItem('login', false);
    }

    static isLogin() {
        let v = sessionStorage.getItem('login');
        return v === "true";
    }

    static isAdmin() {
        let name = sessionStorage.getItem('name');
        return name === "admin";
    }
}

const useAccess = () => {
    const [main, setMain] = useState();

    useEffect(() => {
        const ret = new Set();

        let access = User.userAccess();
        if (access) {
            access = access.split(",");
            access.forEach(d => ret.add(d));
        }

        setMain(ret);
    }, []);

    const has_access = (key) => {
        if (User.isAdmin()) {
            return true;
        }

        if (!main) {
            return true;
        }

        return main.has(key);
    };

    const no_access = (key) => !has_access(key);

    return [no_access, has_access];
};

const utc_offset = () => {
    const cur = new Date();
    const off = cur.getTimezoneOffset();
    const ret = -off / 60;
    return ret;
};

export {utc_offset, useAccess, CurModel, User};
